import Search from 'models/search';

import moment from 'moment';

export default class GoodsActivitySearch extends Search {
  goodsName = '';
  activityTypeId = '';
  branchId = '';
  partnerId = '';
  activityStartTime = null;
  activityEndTime = null;
  status = '';

  propertyList = ['goodsName', 'activityTypeId', 'branchId', 'partnerId', 'activityStartTime', 'activityEndTime', 'status'];

  constructor() {
    super();
    this.setOldData();
  }

  checkValid(){
    let activityStartTime = this.activityStartTime;
    let activityEndTime = this.activityEndTime;
    if (activityEndTime === null && activityStartTime === null) {
      return '';
    }

    if (activityStartTime === null) {
      return 'activityEndTime';
    }

    if (activityEndTime === null) {
      return 'activityEndTime';
    }

    if (moment(activityStartTime).isAfter(activityEndTime, 'second')) {
      return 'startTime>endTime';
    }
    return '';
  }

  getObject(pagination){
    let postData = {};
    let fields = ['goodsName', 'activityTypeId', 'branchId', 'partnerId', 'status'];
    this.setPostData(postData, fields);

    let startTime = this.activityStartTime;
    if (startTime !== null) {
      postData.activityStartTime = moment(startTime).format('YYYY-MM-DD HH:mm:ss');
    }

    let endTime = this.activityEndTime;
    if (endTime !== null) {
      postData.activityEndTime = moment(endTime).format('YYYY-MM-DD HH:mm:ss');
    }

    if (pagination !== undefined) {
      let {
        limit,
        page
      } = pagination.getObject();
      postData.limit = limit;
      postData.page = page;
    }
    return postData;
  }
}
